﻿//==============================================================================
// File: Operation.cs
// Created: 2010-06-07
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System.ComponentModel;

namespace WpfRcp.Core.Operations
{
    /// <summary>
    /// Represents command that can be executed on the database server or on the client.
    /// </summary>
    public abstract class Operation : INotifyPropertyChanged, IOperation
    {
        private string m_Text;
        private string m_Description;

        /// <summary>
        /// Raised whenever property of the operation changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
        
        /// <summary>
        /// Gets the command associated with this operation.
        /// </summary>
        public string Command { get; protected set; }
        
        /// <summary>
        /// Gets or sets command's result. Please bear in mind that not all commands return a result.
        /// </summary>
        public object Result { get; protected set; }
        
        /// <summary>
        /// Returns true if operation can be cancelled. False otherwise.
        /// </summary>
        public virtual bool IsCancelable { get { return false; } }

        /// <summary>
        /// Returns true if operation can be fired and leaved in foreground. False otherwise.
        /// </summary>
        public virtual bool IsBackgroundable { get { return false; } }
        
        /// <summary>
        /// Returns display text.
        /// </summary>
        public string Text
        {
            get { return m_Text; }
            protected set
            {
                m_Text = value;
                OnPropertyChanged("Text");
            }
        }

        /// <summary>
        /// Returns display description.
        /// </summary>
        public string Description
        {
            get { return m_Description; }
            protected set
            {
                m_Description = value;
                OnPropertyChanged("Description");
            }
        }

        /// <summary>
        /// Method that encapsulates operation logic.
        /// </summary>
        public abstract void Execute();

        /// <summary>
        /// Raises <see cref="PropertyChanged"/> event.
        /// </summary>
        /// <param name="propertyName">Property name for which the event is raised.</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}